#!/usr/bin/env python
# -*- coding:utf-8 -*-
from datetime import datetime


class SubjectMesObj:
    READ_STATUS = {"READ": "1", "UNREAD": "0", "DROP": "9"}

    col_keys = ["subject_detl", "res", "status", "create_time"]
    col_def_val = ["", "", 0, None]

    id: 0
    subject_detl: ""
    res: ""
    status: 0
    create_time: datetime

    def __init__(self):
        return

    @staticmethod
    def make_subject_data(data):
        for item_index in range(len(SubjectMesObj.col_keys)):
            try:
                _check_val = data[str(SubjectMesObj.col_keys[item_index]).lower()]
                if (isinstance(_check_val, int) or isinstance(_check_val, float)):
                    if _check_val is None:
                        _val = SubjectMesObj.col_def_val[item_index]
                    else:
                        if isinstance(_check_val, int):
                            _val = int(_check_val)
                        elif isinstance(_check_val, float):
                            _val = float(_check_val)
                        else:
                            _val = round(_check_val, 2)
                else:
                    if _check_val is not None and len(_check_val) > 0:
                        _val = data[str(SubjectMesObj.col_keys[item_index]).lower()]  # 这里获取页面数值
                    else:
                        _val = SubjectMesObj.col_def_val[item_index]  # 这里配置数据库默认值
            except KeyError as e:
                _val = ''
            data[str(SubjectMesObj.col_keys[item_index]).upper()] = _val

    @staticmethod
    def split_params(params):
        g_param = []
        if params['subject_detl'] is not None and len(params['subject_detl']) > 0:
            g_param.append('`subject_detl`=' + "\'" + params['subject_detl'] + "\'")
        if params['res'] is not None and len(params['res']) > 0:
            g_param.append('`res`=' + "\'" + params['res'] + "\'")
        if params['status'] is not None and len(params['status']) > 0:
            g_param.append('`status`=' + "\'" + params['status'] + "\'")
        if params['create_time'] is not None and len(params['create_time']) > 0:
            g_param.append('`create_time`=' + "\'" + params['create_time'] + "\'")
        return ', '.join(str(i) for i in g_param)

    @staticmethod
    def concat_datas(g_param):
        # 获取当前时间
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        arry = [g_param["subject_detl"], g_param["res"], g_param["status"], current_time]
        _res = []
        for _val in arry:
            if isinstance(_val, str):
                _res.append(f"\'{_val}\'")
            else:
                if _val is None:
                    _res.append("null")
                else:
                    _res.append(_val)
        return ', '.join(str(i) for i in _res)

    @staticmethod
    def load_db_2_dict(data):
        import json
        g_param = {}
        g_param["id"] = data[0]
        # g_param["subject_detl"] = data[1]
        g_param["res"] = data[2]
        g_param["status"] = data[3]
        g_param["create_time"] = str(data[4])
        str_detail = data[1]
        detail_obj = json.loads(str_detail)
        g_param["uname"] = detail_obj['name']
        g_param['phone'] = detail_obj['phone']
        g_param['mail'] = detail_obj['email']
        g_param['message'] = detail_obj['message']
        return g_param

    @staticmethod
    def to_string(with_flg):
        g_param = []
        if with_flg:
            g_param.append("`mem_subject_advice`.`subject_detl`")
            g_param.append("`mem_subject_advice`.`res`")
            g_param.append("`mem_subject_advice`.`status`")
            g_param.append("`mem_subject_advice`.`create_time`")
        else:
            g_param.append("`subject_detl`")
            g_param.append("`res`")
            g_param.append("`status`")
            g_param.append("`create_time`")
        return ', '.join(str(i) for i in g_param)
